package com.example.util;

import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 榛子云配置
 * 短信验证码
 */
@Component
public class ZhenziUtil {
    @Value("${zhenZi.apiUrl}")
    private String apiUrl;
    @Value("${zhenZi.appId}")
    private String appId;
    @Value("${zhenZi.appSecret}")
    private String appSecret;
    @Value("${zhenZi.templateId}")
    private String templateId;

    public String printRandom() {
        //取随机产生的验证码(6位数字)
        Random rnd = new Random();
        int randNum = rnd.nextInt(899999) + 100000; //从100000开始，保证6位
        //将整型数字转化成字符串
        String randStr = String.valueOf(randNum);
        return randStr;
    }

    public String sendMessage(String randNum, String phoneNum) throws Exception {
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
        Map<String, Object> params = new HashMap<>();
        params.put("number", phoneNum);
        params.put("templateId", templateId);
        String[] templateParams = new String[2];
        templateParams[0] = randNum;
        templateParams[1] = "5分钟";
        params.put("templateParams", templateParams);
        System.out.println();
        String result = client.send(params);
        return result;
    }

}